package com.yscp.ysdemo.controller;

import com.yscp.ysdemo.constant.ResultEnum;
import com.yscp.ysdemo.dto.ResponseResult;
import com.yscp.ysdemo.entity.User;
import com.yscp.ysdemo.exception.BusinessException;
import com.yscp.ysdemo.service.LoginService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    //login是框架默认路径，不能被覆盖，这里换个名
    @PostMapping("/user/userLogin")
    private ResponseResult doLogin(String username,String password,String captchaMsg){
       if (username == null || password == null || captchaMsg == null){
           throw new BusinessException(ResultEnum.PARAM_ERROR);
       }
        User user = new User(username, password);
        ResponseResult responseResult = loginService.login(user, captchaMsg);
        return responseResult;
    }

    @GetMapping("/user/registeredCheck")
    private ResponseResult registeredCheck(String username){
      return  loginService.registeredCheck(username);
    }



    @PostMapping("/user/registered")
    //存储注册用户信息，重定向
    private ResponseResult doRegistered(String username,String password,String captchaMsg){
        if (username == null || password == null || captchaMsg == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR);
        }
        User user = new User(username, password);
        ResponseResult responseResult = loginService.registered(user, captchaMsg);
        return responseResult;
    }
}
